#pragma once

#include <atomic>
#include <mutex>
#include <vector>
#include <functional>
#include <thread>
#include <memory>
#include <sys/eventfd.h> 

class Channel;
class Poller;

class EventLoop {
public:
    using Functor = std::function<void()>;

    EventLoop();
    ~EventLoop();

    void loop();
    void quit();

    void runInLoop(Functor cb);
    void queueInLoop(Functor cb);

    void updateChannel(Channel* channel);
    void removeChannel(Channel* channel);

private:
    void handleRead();
    void wakeup();
    void doPendingFunctors();

    bool looping_;
    bool quit_;
    const pid_t threadId_;  // 当前线程 ID

    int wakeupFd_;
    std::unique_ptr<Channel> wakeupChannel_;

    std::mutex mutex_;
    std::vector<Functor> pendingFunctors_;
    bool callingPendingFunctors_ = false;

    std::unique_ptr<Poller> poller_;
    std::vector<Channel*> activeChannels_;
};

